package com.imooc.rabbitmq.confirm;

import com.imooc.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    private static final String exchangeName = "test_confirm_exchange";
    private static final String routingKey = "confirm.save";
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();
        //指定消息的确认模式
        channel.confirmSelect();

        String msg = "hello rabbitMq send confirm message!";
        channel.basicPublish(exchangeName,routingKey,null,msg.getBytes());

        channel.addConfirmListener(new ConfirmListener() {
            @Override
            public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("-------------Ack!--------------");
            }

            @Override
            public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("-------------Non Ack!--------------");
            }
        });

    }
}
